// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	_before: function() { // 通用预处理器

	},
	/* 收藏 */
	favorite: async function(article_id, article_type = 0, is_favorite = false) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		let reference = dbJQL.collection('favorite')
		if (is_favorite) {
			return await reference.where({
				article_id,
				article_type,
				user_id: dbJQL.getCloudEnv('$cloudEnv_uid')
			}).remove()
		} else {
			return await reference.add({
				article_id,
				article_type
			})
		}
	},
	/* 收藏列表 资讯 + 圈子 */
	list: async function(pageNo, pageSize) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		const dbCmd = dbJQL.command
		const $ = dbCmd.aggregate
		return await dbJQL.collection('favorite').aggregate()
			.match({
				user_id: dbJQL.getCloudEnv('$cloudEnv_uid'),
				article_type: dbCmd.in([0, 1])
			})
			.sort("createTime", "desc")
			.lookup({
				from: 'consult',
				let: {
					article_id: "$article_id"
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(["$_id", "$$article_id"]))).done(),
				as: 'article'
			})
			.lookup({
				from: 'circle',
				let: {
					circle_id: "$article_id"
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(["$_id", "$$circle_id"])))
					.lookup({
						from: 'uni-id-users',
						let: {
							user_id: '$user_id'
						},
						pipeline: $.pipeline().match(dbCmd.expr($.eq(['$_id', '$$user_id']))).done(),
						as: 'userInfo'
					})
					.project({
						content: 1,
						imageUrl: 1,
						like_count: 1,
						view_count: 1,
						createTime: 1,
						userInfo: $.arrayElemAt(['$userInfo', 0])
					})
					.done(),
				as: 'circle'
			})
			.skip((pageNo - 1) * pageSize)
			.limit(pageSize)
			.project({
				article: $.arrayElemAt(['$article', 0]),
				circle: $.arrayElemAt(['$circle', 0]),
				article_type: 1,
				create_date: 1
			})
			.end()
	},
	// 收藏 - 我的心得
	async favorite_circle_list(pageNo, pageSize) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo(),
		});
		const dbCmd = dbJQL.command
		const $ = dbCmd.aggregate
		return await dbJQL.collection('favorite').aggregate()
			.match({
				user_id: dbJQL.getCloudEnv('$cloudEnv_uid'),
				article_type: 1
			})
			.sort("createTime", "desc")
			.lookup({
				from: 'circle',
				let: {
					article_id: "$article_id"
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(["$_id", "$$article_id"])))
					.lookup({
						from: 'uni-id-users',
						let: {
							user_id: "$user_id"
						},
						pipeline: $.pipeline().match(dbCmd.expr($.eq(['$_id', '$$user_id'])))
							.project({
								nickname: 1,
								avatar: 1
							}).done(),
						as: 'userInfo'
					})
					.project({
						userInfo: $.arrayElemAt(['$userInfo', 0]),
						content: 1,
						imageUrl: 1,
						like_count: 1,
						view_count: 1,
						createTime: 1,
						flag: 1
					})
					.done(),
				as: 'circle'
			})
			.project({
				circle: $.arrayElemAt(['$circle', 0]),
				article_type: 1,
				create_date: 1
			})
			.match({
				'circle.flag': false
			})
			.skip((pageNo - 1) * pageSize)
			.limit(pageSize)
			.end()
	},
	/* 练习- 收藏 */
	async favorite_topic(pageNo, pageSize) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo(),
		});
		const dbCmd = dbJQL.command
		const $ = dbCmd.aggregate

		let skip = (pageNo - 1) * pageSize
		return await dbJQL.collection('favorite').aggregate()
			.match({
				user_id: dbJQL.getCloudEnv('$cloudEnv_uid'),
				article_type: 2
			})
			.lookup({
				from: 'questions-topic',
				let: {
					article_id: "$article_id"
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(["$_id", "$$article_id"]))).done(),
				as: 'topic'
			})
			.project({
				article_id: 1,
				article_type: 1,
				create_date: 1,
				topic: $.arrayElemAt(['$topic', 0])
			})
			.skip(skip)
			.limit(pageSize)
			.end()
	},
	/* 所有收藏列表 */
	async favorite_topic_all() {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		const dbCmd = dbJQL.command
		const $ = dbCmd.aggregate

		return await dbJQL.collection('favorite').aggregate()
			.match({
				user_id: dbJQL.getCloudEnv('$cloudEnv_uid'),
				article_type: 2
			})
			.lookup({
				from: 'questions-topic',
				let: {
					article_id: "$article_id"
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(["$_id", "$$article_id"])))
					.lookup({
						from: 'topic-options',
						let: {
							topic_id: '$_id'
						},
						pipeline: $.pipeline().match(
							dbCmd.expr($.eq(['$topic_id', '$$topic_id']))
						).done(),
						as: 'options'
					})
					.lookup({
						from: 'favorite',
						let: {
							article_id: '$_id'
						},
						pipeline: $.pipeline().match(
							dbCmd.expr($.eq(['$article_id', '$$article_id']))
						).count('count').done(),
						as: 'is_like'
					})
					.lookup({
						from: 'opendb-note',
						let: {
							topic_id: '$_id'
						},
						pipeline: $.pipeline().match(
							dbCmd.expr($.and([
								$.eq(['$topic_id', '$$topic_id']),
								$.eq(['$flag', false]),
								$.eq(['$user_id', dbJQL.getCloudEnv('$cloudEnv_uid')])
							]))
						).done(),
						as: 'note'
					})
					.project({
						analysis: 1,
						answer: 1,
						difficulty: 1,
						options: 1,
						question_content: 1,
						question_storehouse_id: 1,
						question_type: 1,
						is_like: $.cond({
							if: $.gt([$.arrayElemAt(['$is_like.count', 0]), 0]),
							then: true,
							else: false
						}),
						note: $.arrayElemAt(['$note', 0])
					})
					.done(),
				as: 'topic'
			})
			.project({
				article_id: 1,
				topic: $.arrayElemAt(['$topic', 0])
			})
			.end()
	}
}